package com.ns.dcjh.douyinapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.bytedance.sdk.open.aweme.CommonConstants;
import com.bytedance.sdk.open.aweme.authorize.model.Authorization;
import com.bytedance.sdk.open.aweme.common.handler.IApiEventHandler;
import com.bytedance.sdk.open.aweme.common.model.BaseReq;
import com.bytedance.sdk.open.aweme.common.model.BaseResp;
import com.bytedance.sdk.open.aweme.share.Share;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;
import com.ns.dcjh.bean.MessageEvent;
import com.ns.dcjh.constant.Constant;

import org.greenrobot.eventbus.EventBus;


public class DouYinEntryActivity extends Activity implements IApiEventHandler {

    private DouYinOpenApi douYinOpenApi;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        douYinOpenApi = DouYinOpenApiFactory.create(this);
        if (douYinOpenApi != null) {
            douYinOpenApi.handleIntent(getIntent(), this);
        } else {
            Toast.makeText(DouYinEntryActivity.this,
                    "抖音初始化失败，请重新打开该APP", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == CommonConstants.ModeType.SHARE_CONTENT_TO_TT_RESP) {
            Share.Response response = (Share.Response) resp;

            JSONObject extras = new JSONObject();
            if (response.subErrorCode != 0) {
                extras.put("errorCode", response.subErrorCode);
                extras.put("errorMsg", shareErrorCode(response.subErrorCode));
            } else {
                extras.put("errorCode", response.errorCode);
                extras.put("errorMsg", shareErrorCode(response.errorCode));
            }
            MessageEvent msgEvent = new MessageEvent(Constant.MSG_EVENT_SHARE_CONTENT_TO_TT_RESP);
            msgEvent.setExtras(extras);
            EventBus.getDefault().post(msgEvent);
        }

        if (resp.getType() == CommonConstants.ModeType.SEND_AUTH_RESPONSE) {
            Authorization.Response response = (Authorization.Response) resp;
            JSONObject extras = new JSONObject();
            extras.put("errorCode", response.errorCode);
            extras.put("errorMsg", authErrorCode(response.errorCode));
            extras.put("authCode", response.authCode);
            extras.put("scope", response.grantedPermissions);

            MessageEvent msgEvent = new MessageEvent(Constant.MSG_EVENT_AUTH_DOUYIN_RESP);
            msgEvent.setExtras(extras);
            EventBus.getDefault().post(msgEvent);
        }

        finish();
    }

    @Override
    public void onErrorIntent(@Nullable Intent intent) {
        // 错误数据
        Toast.makeText(this, "Intent出错", Toast.LENGTH_LONG).show();
    }

    public String authErrorCode(int errorCode) {
        switch (errorCode) {
            case -1:
                return "未知错误";
            case -2:
                return "用户手动取消";
            case 7:
                return "访问频繁，命中风控";
            case -3:
                return "网络异常";
            case -12:
                return "网络未连接";
            case -13:
                return "网络连接超时";
            case -14:
                return "网络超时";
            case -15:
                return "网络io错误";
            case -16:
                return "网络未知host错误";
            case -21:
                return "网络ssl错误";
            case 10001:
                return "系统异常";
            case 10002:
                return "参数错误";
            case 10003:
                return "非法的配置（partner_client）";
            case 10004:
                return "授权域配置错误";
            case 10005:
                return "缺少参数";
            case 10006:
                return "非法重定向url";
            case 10007:
                return "Authorization code 失效";
            case 10008:
                return "Access_Token 无效";
            case 10009:
                return "非法参数";
            case 10010:
                return "refresh token 过期";
            case 10011:
                return "应用标识有误，配置的包名和实际的包名不符";
            case 10012:
                return "应用审核中，无法使用，联系运营同学";
            case 10013:
                return "client_key或者client_secret错误";
            case 10014:
                return "客户端携带的client_key不一致";
            case 10015:
                return "应用类型错误，检查是否混用APP/WEB 类型的client_key";
            case 10017:
                return "授权鉴权失败，需要补全签名信息。查看详情 请将您的线上包（发布的应用市场的包）应用安装到手机上之后，再使用签名工具生成签名。";
            default:
                return "未知错误码, authErrorCode错误码为: " + errorCode;
        }
    }


    public String shareErrorCode(int errorCode) {
        switch (errorCode) {
            case -1:
                return "未知错误";
            case -2:
                return "用户手动取消";
            case -3:
                return "发送失败";
            case -4:
                return "第三方未获取分享权限或获取权限失败";
            case -5:
                return "文件解析错误";
            case 20002:
                return "无效请求参数";
            case 20003:
                return "应用包名与官网填写不对应或者无 aweme.share 权限。请检查应用是否正常或者包名是否匹配。";
            case 20004:
                return "用户手动取消登录";
            case 20005:
                return "用户未授权使用相册";
            case 20006:
                return "网络出错";
            case 20007:
                return "视频格式或长度不支持";
            case 20008:
                return "图文格式不支持";
            case 20010:
                return "解析媒体资源失败";
            case 20011:
                return "视频尺寸或比例不支持";
            case 20012:
                return "视频格式不支持（要求 mp4）";
            case 20013:
                return "用户手动取消发布";
            case 20014:
                return "有正在上传的作品，需要发布完成后才能下一次发布";
            case 20015:
                return "用户存为草稿";
            case 20017:
                return "无效的链接";
            case 20018:
                return "无效的用户";
            case 20029:
                return "当前为基本模式";
            case 20030:
                return "当前处于防沉迷时间锁阻断状态";
            case 20031:
                return "当前处于防沉迷蒙层展示状态";
            case 20035:
                return "视频状态非公开";
            case 20036:
                return "当前抖音版本不支持，请升级到最新抖音版本";
            case 20037:
                return "内部异常";
            case 20041:
                return "请检查应用包名是否匹配";
            case 22001:
                return "不支持的分辨率 android 独有";
            default:
                return "未知错误码, shareErrorCode错误码为:" + errorCode;
        }
    }
}
